package cdrprocessing.bolt.stat;

import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Tuple;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ExportStatBolt extends BaseRichBolt {

    private PrintWriter callsPerSecWriter;

    private PrintWriter durationsPerSecWriter;

    private PrintWriter failedCallsPerSecWriter;

    private ObjectMapper jsonParser;

    public void declareOutputFields(OutputFieldsDeclarer declarer) {
    }

    public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
        try {
            {
                final Socket socket = new Socket("localhost", 8181);

                callsPerSecWriter = new PrintWriter(socket.getOutputStream());
                callsPerSecWriter.println("callsPerSec");
                callsPerSecWriter.flush();
            }

            {
                final Socket socket = new Socket("localhost", 8181);

                durationsPerSecWriter = new PrintWriter(socket.getOutputStream());
                durationsPerSecWriter.println("durationsPerSec");
                durationsPerSecWriter.flush();
            }

            {
                final Socket socket = new Socket("localhost", 8181);

                failedCallsPerSecWriter = new PrintWriter(socket.getOutputStream());
                failedCallsPerSecWriter.println("failedCallsPerSec");
                failedCallsPerSecWriter.flush();
            }

            jsonParser = new ObjectMapper();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void execute(Tuple input) {
        if ("CallPerSec".equals(input.getSourceStreamId())) {
            final Map<String, Object> data = new HashMap<String, Object>();
            data.put("count", input.getValueByField("count"));

            try {
                callsPerSecWriter.println(jsonParser.writeValueAsString(data));
                callsPerSecWriter.flush();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else if ("DurationPerSec".equals(input.getSourceStreamId())) {
            final Map<String, Object> data = new HashMap<String, Object>();
            data.put("duration", input.getValueByField("duration"));

            try {
                durationsPerSecWriter.println(jsonParser.writeValueAsString(data));
                durationsPerSecWriter.flush();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else if ("FailCallPerSec".equals(input.getSourceStreamId())) {
            final Map<String, Object> data = new HashMap<String, Object>();
            data.put("count", input.getValueByField("count"));

            try {
                failedCallsPerSecWriter.println(jsonParser.writeValueAsString(data));
                failedCallsPerSecWriter.flush();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            System.out.println("Unexpected tuple " + input);
        }
    }

}
